var upload, layer, form, table;

var cols = [
    {field: 'id', hide: true}
    , {field: 'fileName', width: 200, title: '文件名称', align: 'center'}
    , {field: 'importStatus', width: 130, title: '导入状态', align: 'center'}
    , {field: 'successCount', width: 130, title: '导入成功总笔数', align: 'center'}
    , {field: 'createTime', width: 160, title: '创建时间', align: 'center'}
    , {field: 'finishTime', width: 160, title: '完成时间', align: 'center'}
    , {field: 'importType', hide: true}
    , {field: 'failCount', hide: true}
    , {fixed: 'right', title: '操作', align: 'center', toolbar: '#barOption'}

];

$(function () {
    layui.use(['upload', 'layer', 'form', 'table'], function () {
        upload = layui.upload;
        layer = layui.layer;
        form = layui.form;
        table = layui.table;
        //执行实例
        upload.render({
            elem: '#selectFile',
            headers: {"token": getCookie("token")},
            url: serverPre + "/manager/yzt/v1/upload",
            auto: false,
            accept: 'file',
            exts: 'txt',
            data: {
                type: function () {
                    return $('#importType').val();
                }
            },
            bindAction: '#uploadFile',
            before: function () {
                layer.load(2);
            },
            done: function (res) {
                layer.closeAll('loading');
                if (res.code !== 0) {
                    layer.msg(res.msg, {icon: 1, time: 2000});
                } else {
                    //queryUploadData();
                }
            }
        });

        //queryUploadData();
        table.on('tool(uploadTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'downLoadErrorData') {
                downLoadData(data['importType'], "errorData", data['id']);
            } else if (obj.event === 'downLoadAllData') {
                downLoadData(data['importType'], "allData", data['id']);
            }
        });

        form.render();
    });

    var queryUploadData = function () {
        table.render({
            elem: "#uploadTable",
            height: 500,
            headers: {"token": getCookie("token")},
            cols: [cols],
            url: serverPre + "/manager/yzt/v1/upload/get",
            method: 'get',
            page: true,
            limits: [10, 20, 50],
            limit: 20,
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.pageInfo.totalCount, //解析数据长度
                    "data": res.data.pageInfo.list //解析数据列表
                };
            },
        });
    };

    var downLoadData = function (importType, exportType, importId) {
         window.location.href = serverPre+"/manager/yzt/v1/upload/export?importType="+importType+"&exportType="+exportType+"&importId="+importId;
    };
});